package com.openxt.nbzj.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.openxt.nbzj.R;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.xutils.x;

import qs.openxt.libs.util.StringUtil;

/**
 * Created by tongky on 2016/5/19.
 */
public class NewsWorkListAdapter extends BaseAdapter {
    /** 授权列表 */
    private List<JSONObject> mList = null;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy年MM月dd日");

    /** 上下文对象 **/
    private Context mContext = null;

    private boolean transparent = false;

    public NewsWorkListAdapter(Context context, List<JSONObject> list) {
        mContext = context;
        mList = list;
    }

    public void setTransparent(boolean transparent) {
        this.transparent = transparent;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        JSONObject item = mList.get(position);
        String show_type = item.getString("show_type");
        if(StringUtil.isNull(show_type))
            show_type = "0";
        Context context = parent.getContext();
        ViewHolder viewHolder = null;
        ViewHolderThree viewHolderThree = null;
        if (convertView != null) {
            if("0".equals(show_type) || "1".equals(show_type)) {
                Object obj = convertView.getTag();
                if(obj instanceof ViewHolder)
                    viewHolder = (ViewHolder) convertView.getTag();
                else{
                    convertView = LayoutInflater.from(context).inflate(R.layout.news_work_item, null);
                    viewHolder = new ViewHolder(convertView);
                    convertView.setTag(viewHolder);
                }
            }else if("2".equals(show_type)) {
                Object obj = convertView.getTag();
                if(obj instanceof ViewHolderThree)
                    viewHolderThree = (ViewHolderThree) convertView.getTag();
                else{
                    convertView = LayoutInflater.from(context).inflate(R.layout.news_work_item_three, null);
                    viewHolderThree = new ViewHolderThree(convertView);
                    convertView.setTag(viewHolderThree);
                }
            }
        } else {
            if("0".equals(show_type) || "1".equals(show_type)) {
                convertView = LayoutInflater.from(context).inflate(R.layout.news_work_item, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else if("2".equals(show_type)) {
                convertView = LayoutInflater.from(context).inflate(R.layout.news_work_item_three, null);
                viewHolderThree = new ViewHolderThree(convertView);
                convertView.setTag(viewHolderThree);
            }
        }
        if("0".equals(show_type)) {
            if(transparent) {
                viewHolder.item.setBackgroundResource(R.color.transparent);
                viewHolder.title.setTextColor(ContextCompat.getColor(context, R.color.white));
                viewHolder.ARTICLE_DT.setTextColor(ContextCompat.getColor(context, R.color.white));
                viewHolder.introduction.setTextColor(ContextCompat.getColor(context, R.color.white));
                viewHolder.typename.setTextColor(ContextCompat.getColor(context, R.color.white));
                viewHolder.typename.setBackgroundResource(R.drawable.bg_w_ellipse);
            }else {
                viewHolder.item.setBackgroundResource(R.color.white);
                viewHolder.title.setTextColor(ContextCompat.getColor(context, R.color.text_g));
                viewHolder.ARTICLE_DT.setTextColor(ContextCompat.getColor(context, R.color.text_54));
                viewHolder.introduction.setTextColor(ContextCompat.getColor(context, R.color.text_7));
                viewHolder.typename.setTextColor(ContextCompat.getColor(context, R.color.text_54));
                viewHolder.typename.setBackgroundResource(R.drawable.bg_w_b_ellipse);
            }
            viewHolder.title.setText(item.getString("TITLE"));
            viewHolder.introduction.setText(item.getString("introduction"));
            try {
                viewHolder.ARTICLE_DT.setText(dateFormat1.format(dateFormat.parse(item.getString("ARTICLE_DT"))));
            }catch (Exception e){

            }
            viewHolder.typename.setText(item.getString("typename"));
            viewHolder.img.setVisibility(View.GONE);
        }else if("1".equals(show_type)) {
            if(transparent) {
                viewHolder.item.setBackgroundResource(R.color.transparent);
                viewHolder.title.setTextColor(ContextCompat.getColor(context, R.color.white));
                viewHolder.ARTICLE_DT.setTextColor(ContextCompat.getColor(context, R.color.white));
                viewHolder.introduction.setTextColor(ContextCompat.getColor(context, R.color.white));
                viewHolder.typename.setTextColor(ContextCompat.getColor(context, R.color.white));
                viewHolder.typename.setBackgroundResource(R.drawable.bg_w_ellipse);
            }else {
                viewHolder.item.setBackgroundResource(R.color.white);
                viewHolder.title.setTextColor(ContextCompat.getColor(context, R.color.text_g));
                viewHolder.ARTICLE_DT.setTextColor(ContextCompat.getColor(context, R.color.text_54));
                viewHolder.introduction.setTextColor(ContextCompat.getColor(context, R.color.text_7));
                viewHolder.typename.setTextColor(ContextCompat.getColor(context, R.color.text_54));
                viewHolder.typename.setBackgroundResource(R.drawable.bg_w_b_ellipse);
            }
            viewHolder.title.setText(item.getString("TITLE"));
            viewHolder.introduction.setText(item.getString("introduction"));
            try {
                viewHolder.ARTICLE_DT.setText(dateFormat1.format(dateFormat.parse(item.getString("ARTICLE_DT"))));
            }catch (Exception e){

            }
            viewHolder.typename.setText(item.getString("typename"));
            JSONArray IMGS = item.getJSONArray("images");
            if(IMGS != null) {
                String IMGURL = IMGS.getString(0);
                if (StringUtil.isNull(IMGURL))
                    viewHolder.img.setImageResource(R.mipmap.default_img);
                else
                    x.image().bind(viewHolder.img, IMGURL);
                viewHolder.img.setVisibility(View.VISIBLE);
            }
        }else if("2".equals(show_type)) {
            if(transparent) {
                viewHolderThree.item.setBackgroundResource(R.color.transparent);
                viewHolderThree.title.setTextColor(ContextCompat.getColor(context, R.color.white));
            }else {
                viewHolderThree.item.setBackgroundResource(R.color.white);
                viewHolderThree.title.setTextColor(ContextCompat.getColor(context, R.color.text_g));
            }
            viewHolderThree.title.setText(item.getString("TITLE"));
            JSONArray IMGS = item.getJSONArray("images");
            if(IMGS != null) {
                String IMGURL = IMGS.getString(0);
                if (StringUtil.isNull(IMGURL))
                    viewHolderThree.img0.setImageResource(R.mipmap.default_img);
                else
                    x.image().bind(viewHolderThree.img0, IMGURL);
                IMGURL = IMGS.getString(1);
                if (StringUtil.isNull(IMGURL))
                    viewHolderThree.img1.setImageResource(R.mipmap.default_img);
                else
                    x.image().bind(viewHolderThree.img1, IMGURL);
                IMGURL = IMGS.getString(2);
                if (StringUtil.isNull(IMGURL))
                    viewHolderThree.img2.setImageResource(R.mipmap.default_img);
                else
                    x.image().bind(viewHolderThree.img2, IMGURL);
            }
        }

        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.item)
        RelativeLayout item;
        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.introduction)
        TextView introduction;
        @BindView(R.id.ARTICLE_DT)
        TextView ARTICLE_DT;
        @BindView(R.id.typename)
        TextView typename;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    class ViewHolderThree {
        @BindView(R.id.item)
        RelativeLayout item;
        @BindView(R.id.img0)
        ImageView img0;
        @BindView(R.id.img1)
        ImageView img1;
        @BindView(R.id.img2)
        ImageView img2;
        @BindView(R.id.title)
        TextView title;

        ViewHolderThree(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

